package com.wwx.worldonwheels.service.impl;

import com.wwx.worldonwheels.VO.CarVO;
import com.wwx.worldonwheels.VO.ServiceVO;
import com.wwx.worldonwheels.common.Constant;
import com.wwx.worldonwheels.common.result.CommonResult;
import com.wwx.worldonwheels.domain.*;
import com.wwx.worldonwheels.mapper.*;
import com.wwx.worldonwheels.mapper.WwxCustomerMapper;
import com.wwx.worldonwheels.mapper.WwxInvoiceMapper;
import com.wwx.worldonwheels.mapper.WwxServiceMapper;
import com.wwx.worldonwheels.mapper.WwxVehicleMapper;
import com.wwx.worldonwheels.service.CarRentalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Objects;

/**
 * ClassName: CarRentalServiceImpl
 * Description:
 * CreatedBy: HuaHX
 */
@Service
public class CarRentalServiceImpl implements CarRentalService {


    @Autowired
    private WwxVehicleMapper vehicleMapper;
    @Autowired
    private WwxServiceMapper serviceMapper;
    @Autowired
    private WwxCustomerMapper customerMapper;
    @Autowired
    private WwxIcoupMapper icoupMapper;
    @Autowired
    private WwxCcoupMapper ccoupMapper;
    @Autowired
    private WwxDiscountMapper discountMapper;
    @Override
    public CommonResult ListCar(String orderBy) {
        if (orderBy.equals("class"))
            orderBy = "name";
        if (orderBy.equals("dailyrate"))
            orderBy = "daily_rate";
        CarVO[] cars = vehicleMapper.ListCar(orderBy);
        if (cars == null || cars.length == 0) {
            return CommonResult.failed("no available car");
        }
        return CommonResult.success(cars);
    }

    @Override
    public CommonResult ListService(String id) {
        ServiceVO[] services = serviceMapper.ListService(id);
        if (services == null || services.length == 0) {
            return CommonResult.failed("service not exist", id);
        }
        return CommonResult.success(services);
    }

    @Override
    public CommonResult FindCar(Integer vehicleClass) {
        WwxVehicleExample customerExample = new WwxVehicleExample();
        customerExample.or().andUseStatusEqualTo(Constant.VEHICLE_NOT_USE).andWwxClassClassIdEqualTo(vehicleClass);
        return CommonResult.success(vehicleMapper.selectByExample(customerExample));
    }
    
    @Autowired
    private WwxInvoiceMapper invoiceMapper;

    @Override
    @Transactional
    public CommonResult BorrowCar(String phone, String vin) {
        //find Car
        WwxVehicle vehicle = vehicleMapper.selectByPrimaryKey(vin);
        //change  car use status
        if (Constant.VEHICLE_NOT_USE.equals(vehicle.getUseStatus())) {
            vehicle.setUseStatus(Constant.VEHIICLE_IN_USE);
        } else {
            return CommonResult.failed("the car is already in use");
        }

        int res = vehicleMapper.updateByPrimaryKeySelective(vehicle);
        if (res == 0) {
            return CommonResult.failed();
        }
        WwxService service = new WwxService();

        WwxCustomer customer = customerMapper.selectByPhone(phone);
        if (customer == null) {
            return CommonResult.failed("this user is not exist", phone);
        }
        //insert service vin,custid
        service.setWwxVehicleVin(vin);
        service.setWwxCustomerCustId(customerMapper.selectByPhone(phone).getCustId());

        serviceMapper.insertSelective(service);
//update invoiceId
        WwxInvoiceExample invoiceExample = new WwxInvoiceExample();
        invoiceExample.or().andWwxServiceServIdEqualTo(service.getServId());
        WwxInvoice invoice = invoiceMapper.selectByExample(invoiceExample).get(0);
        service.setWwxInvoiceInvoiceId(invoice.getInvoiceId());
        serviceMapper.updateByPrimaryKeySelective(service);


        return CommonResult.success(service);
    }

    @Override
    public CommonResult findCoupon(Integer id) {
        WwxCustomer customer = customerMapper.selectByPrimaryKey(id);
        List<WwxDiscount> discountList = new LinkedList<>();
        if (customer.getCustType().equals("0")) {
            String[] couPonId = icoupMapper.selectByCustomerId(id);
            for (int i = 0; i < couPonId.length; i ++ ) {
                discountList.add(discountMapper.selectByPrimaryKey(couPonId[i]));
            }
            if (discountList.size() == 0) {
                return CommonResult.failed("no coupons found!");
            } else {
                return CommonResult.success(discountList);
            }
        } else if (customer.getCustType().equals("1")){
            String[] couPonId = ccoupMapper.selectByCustomerId(id);
            for (int i = 0; i < couPonId.length; i ++ ) {
                discountList.add(discountMapper.selectByPrimaryKey(couPonId[i]));
            }
            if (discountList.size() == 0) {
                return CommonResult.failed("no coupons found!");
            } else {
                return CommonResult.success(discountList);
            }
        }
        return CommonResult.failed("no coupons found!");
    }

}
